package com.cloudea.lotus.utils;

import java.util.HashMap;
import java.util.Map;

public class ParameterParser {
    /**
     * 将URL参数字符串解释成key value的字典
     * @param text 要解析的字符串
     * @return 解析完成的字典
     */
    public Map<String, String> parse(String text){
        Map<String, String> ret = new HashMap<String, String>();
        if(text != null && !text.equals("")) {
            String[] keyValues = text.split("&");
            for(String keyValue : keyValues) {
                try {
                    String[] keyAndValue = keyValue.split("=");
                    String key = keyAndValue[0].trim();
                    String val = keyAndValue.length > 1 ? keyAndValue[1].trim() : "";
                    ret.put(key, val);
                }catch(Exception e) {
                    continue;
                }
            }
        }
        return ret;
    }
}